package com.module.wx.util;

import com.module.wx.domain.ZzdOrderListInfo;
import com.module.wx.domain.ZzdOrgInfo;
import com.module.wx.domain.ZzdStoreInfo;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 工具类
 */
public class Util {

    /**
     * 根据时间戳生成主键id
     * @return
     */
    public static String get_radom_id(){
     return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSSSSS")).toString();
    }

    /**
     * String转date类型
     * @param date_str
     * @return
     */
    public static Date fomat_date(String date_str){
        Date date = null;
        //实现将字符串转成⽇期类型
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(date_str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 用于处理sql返回的数据,返回格式为list格式（添加菜品时模糊匹配商家）
     * @param sql
     * @return
     * @throws SQLException
     */
    public static List<ZzdStoreInfo> getMysqlConnectionResult(String sql) throws SQLException, ClassNotFoundException {
        List<ZzdStoreInfo> list = new ArrayList<ZzdStoreInfo>();
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ruoyi_system", "root", "root123");
        Statement stmt =conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()){
            //将数据实体循环添加到list中
            ZzdStoreInfo zzdStoreInfo = new ZzdStoreInfo();
            zzdStoreInfo.setId(rs.getString("id"));
            zzdStoreInfo.setNickName(rs.getString("nickName"));
            list.add(zzdStoreInfo);
        }
        rs.close();
        stmt.close();
        conn.close();
        return list;
    }

    /**
     * 获取所有三级机构信息
     * @param sql
     * @return
     * @throws SQLException
     */
    public static List<ZzdOrgInfo> getOrgListData(String sql) throws SQLException, ClassNotFoundException {
        List<ZzdOrgInfo> list = new ArrayList<ZzdOrgInfo>();
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ruoyi_system", "root", "root123");
        Statement stmt =conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()){
            //将数据实体循环添加到list中
            ZzdOrgInfo zzdOrgInfo = new ZzdOrgInfo();
            zzdOrgInfo.setId(rs.getString("id"));
            zzdOrgInfo.setOrgName(rs.getString("orgName"));
            list.add(zzdOrgInfo);
        }
        rs.close();
        stmt.close();
        conn.close();
        return list;
    }

    /**
     * 获取所有三级机构信息
     * @param sql
     * @return
     * @throws SQLException
     */
    public static List<ZzdOrderListInfo> getOrderListDataByOrderNo(String sql) throws SQLException, ClassNotFoundException {
        List<ZzdOrderListInfo> list = new ArrayList<ZzdOrderListInfo>();
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ruoyi_system", "root", "root123");
        Statement stmt =conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()){
            //将数据实体循环添加到list中
            ZzdOrderListInfo zzdOrderListInfo = new ZzdOrderListInfo();
            zzdOrderListInfo.setOrderNo(rs.getString("order_no"));
            zzdOrderListInfo.setId(rs.getString("id"));
            zzdOrderListInfo.setFoodId(rs.getString("food_id"));
            zzdOrderListInfo.setFoodName(rs.getString("food_name"));
            zzdOrderListInfo.setFoodPic(rs.getString("food_pic"));
            zzdOrderListInfo.setFoodPrice(rs.getFloat("food_price"));
            zzdOrderListInfo.setFoodStyle(rs.getString("food_style"));
            zzdOrderListInfo.setOrderSum(rs.getFloat("order_sum"));
            zzdOrderListInfo.setRemarks(rs.getString("remarks"));
            zzdOrderListInfo.setStoreId(rs.getString("store_id"));
            zzdOrderListInfo.setStoreName(rs.getString("store_name"));
            list.add(zzdOrderListInfo);
        }
        rs.close();
        stmt.close();
        conn.close();
        return list;
    }
}
